package com.daigou.tools;

import com.daigou.model.User;


public class AuthSqlBuilder {
	
	public static final Long ALL_AUTH_TAG = 100L;
	
	/**
	 * 构建通用角色sql
	 * @param resourceId
	 * @param tableAlias
	 * @return
	 */
	public static StringBuilder buildAuthSql(User user,String tableAlias){
		//设置权限0,超级管理员,1，店铺管理员，2，业务经理，3，业务员
		StringBuilder strBld = new StringBuilder();
		if(user.getType() == 2){
			strBld.append(" (").append(tableAlias).append("creator_id = ").append(user.getId());
			//默认100是没有任何归属的数据
			strBld.append(" or ").append(tableAlias).append("creator_id = ").append(ALL_AUTH_TAG);
			strBld.append(" or exists (");
			strBld.append("select 1 from tb_user tu where tu.leader_id = ").append(user.getId());
			strBld.append(" and tu.id = ").append(tableAlias).append("creator_id");
			strBld.append(")");
			strBld.append(")");
		}else if(user.getType() == 3){
			strBld.append(" (").append(tableAlias).append("creator_id = ").append(user.getId());
			strBld.append(" or ").append(tableAlias).append("creator_id = ").append(ALL_AUTH_TAG);;
			strBld.append(")");
		}
		return strBld;
	}
	public static void main(String[] args){
		User user = new User();
		user.setType(2);
		System.out.println(buildAuthSql(user,"o"));
	}
}
